//
//  HTTPService.swift
//  SwiftWeibo
//
//  Created by enway-wk on 2017/6/21.
//  Copyright © 2017年 enway-wk. All rights reserved.
//

import UIKit
import AFNetworking
/// 请求方法
///
/// - GET:  get
/// - POST: post
enum HTTPRequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class HTTPService: AFHTTPSessionManager {
    
    static let shared = HTTPService()
    
    func request(method:HTTPRequestMethod = .GET,URLString:String,parameters:[String:AnyObject],completion:@escaping (_ json:AnyObject?,_ isSuccess:Bool)->()){
        
        //成功回调
        let success = {(task:URLSessionDataTask,json:Any?)->() in
            completion(json as AnyObject,true)
        }
        
        //失败回调
        let failure = {(task:URLSessionDataTask?,error:Error)->() in
            print("网络请求错误\(error)")
            completion(error as AnyObject, false)
        }
        
        if method == .GET {
            get(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            post(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
 
    }
}





